//
//  Hot_countryTableViewCell.m
//  AroundTheWorld
//
//  Created by dllo on 16/5/18.
//  Copyright © 2016年 IfelseGo an. All rights reserved.
//

#import "Hot_countryTableViewCell.h"
#import "Hot_countryCollectionViewCell.h"

#define WIDTH [UIScreen mainScreen].bounds.size.width

@implementation Hot_countryTableViewCell
//自定义初始化
-(instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        [self addView];
        self.smallArray = [NSMutableArray array];
    }
    return self;
}
- (void)addView
{
    UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc] init];
    flowLayout.itemSize = CGSizeMake((WIDTH - 50) / 2, (WIDTH - 50) * 230 / 300);
    flowLayout.sectionInset = UIEdgeInsetsMake(20, 20, 20, 20);
    UICollectionView *collectionV = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 0, 0, 0) collectionViewLayout:flowLayout];
    
//    collectionV.backgroundColor = [UIColor whiteColor];
    collectionV.dk_backgroundColorPicker = DKColorPickerWithRGB(0xffffff,0xCFCFCF);
    collectionV.bounces = NO;
    collectionV.showsVerticalScrollIndicator = NO;
    [self.contentView addSubview:collectionV];
    
    //成为代理人
    collectionV.delegate = self;
    collectionV.dataSource = self;
    
    //注册
    [collectionV registerClass:[Hot_countryCollectionViewCell class] forCellWithReuseIdentifier:@"reuse"];
    self.myCollectionV = collectionV;
}
//cell数
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return self.smallArray.count;
}
//建cell
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    Hot_countryCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"reuse" forIndexPath:indexPath];
    countryModel *model = [self.smallArray objectAtIndex:indexPath.row];
    [cell.photoImageV sd_setImageWithURL:[NSURL URLWithString:model.photo] placeholderImage:[UIImage imageNamed:@""]];
    cell.cnnameLabel.text = model.cnname;
    cell.ennameLabel.text = model.enname;
    cell.countLabel.text = [NSString stringWithFormat:@"%ld",(long)model.count];
    cell.labelLabel.text = model.label;
    return cell;
}

- (void) setSmallArray:( NSMutableArray *) smallArray
{
    _smallArray = smallArray;
    [self.myCollectionV reloadData];
}

- (void)layoutSubviews {
    [super layoutSubviews];
    self.myCollectionV.frame = self.contentView.frame;
}

//点击触发方法

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    countryModel *model =[self.smallArray objectAtIndex:indexPath.row];
    self.myBlock(model.countryID);
}

- (void)awakeFromNib {
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
